package logic;

import dk.ange.octave.OctaveEngine;
import dk.ange.octave.OctaveEngineFactory;

public class OctaveEngineer 
{
 private static OctaveEngine octave=null;
 private static int called=0;
 private static int MAX_CALLED=60000;
 private OctaveEngineer()
 {
	 octave=new OctaveEngineFactory().getScriptEngine();
 }
 public static OctaveEngine getOctaveEngine()
 {
	 if(octave==null)
	  {
		 octave=new OctaveEngineer().octave;
		 octave.eval("mat=eye(2);");
		 octave.eval("H=1/sqrt(2)*[1 1;1 -1];H=convertToSu2(H);");
		 octave.eval("T=[exp(-1i*pi/8) 0;0 exp(1i*pi/8)];");
	  }
	 called++;
	 if(called>=MAX_CALLED)
	 {
		 octave.eval("save workspace.mat;");
		 octave.close();
		 called=0;
		 octave=new OctaveEngineer().octave;
		 octave.eval("load workspace.mat;");
	 }
	 return octave;
	  	 
 }
 public static void endOctaveEngine()
 {
	 if(octave!=null)
	  {
		 octave.close();
	  }
	 octave=null;
 }
}
